package logic

import (
	"context"
	"encoding/json"
	"manage/models/mysql"
	"manage/models/redis"
	"time"

	"manage/managerpc/internal/svc"
	"manage/managerpc/managerpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type HotListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewHotListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *HotListLogic {
	return &HotListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 热门列表
func (l *HotListLogic) HotList(in *managerpc.HotListRequest) (*managerpc.HotListResponse, error) {
	// todo: add your logic here and delete this line
	var user []mysql.Activity
	get := redis.Rdb.Get(l.ctx, "liked")
	if get.Val() == "" {
		mysql.DB.Order("liked desc").Limit(10).Find(&user)
		marshal, _ := json.Marshal(user)
		redis.Rdb.Set(l.ctx, "liked", marshal, time.Hour*24)
	} else {
		json.Unmarshal([]byte(get.Val()), &user)
	}
	var Act []*managerpc.HotLists
	for _, n := range user {
		list := managerpc.HotLists{
			Id:     int64(n.ID),
			UserId: int64(n.UserId),
			Title:  n.Title,
			Image:  n.Image,
			Static: n.Static,
			Hot:    int64(n.Hot),
		}
		Act = append(Act, &list)
	}
	return &managerpc.HotListResponse{
		List: Act,
	}, nil
}
